//
//  HistoryView.swift
//  Chatup
//
//  Created by op on 2023/10/17.
//

import SwiftUI
struct TabButton: View {
    var title: String
    @Binding var selectedTab: String
    var action: () -> Void
    var body: some View {
        Button(action: {
            self.action()
        }) {
            Text(title)
                .font(.system(size: 12, weight: .bold))
                .foregroundColor(.white)
                .padding(.vertical, 9)
                .padding(.horizontal, 12)
                .background(selectedTab == title ? Color(red: 0.05, green: 0.75, blue: 0.54) : Color(red: 0.2, green: 0.2, blue: 0.22))
                .cornerRadius(15)
        }
    }
}

struct HistoryView: View {
    @StateObject var viewModel = HistoryViewModel()
    @Binding var section: ContentView.Tab
    @State private var selectedTab = String.i18nText("characters_popular")
    @Binding var historyItemRoleId: Int
    @State var item:MsgItem = .init(text: "")
    @State private var showingChatView = false

    var body: some View {
        GeometryReader(content: { _ in
            NavigationView{
                VStack {
                    HStack {
                        Text(String.i18nText("tab_history"))
                            .font(.system(size: 20, weight: .bold))
                            .foregroundColor(.white)
                    }
                    .padding(.horizontal)
                    .padding(.vertical, 8)
                    ScrollView {
                        VStack {
                            if viewModel.showVip {
                                Button(action: {
                                    viewModel.logFreeClick()
                                    viewModel.logfrom = "history_freehistory"
                                    viewModel.showSetting.toggle()
                                }, label: {
                                    VipBarView()
                                        .frame(height: 84)
                                    //                                    .onTapGesture {
                                    //                                        viewModel.showSetting.toggle()
                                    //                                    }
                                })
                                Spacer()
                                    .frame(height: 22)
                            }
                            ScrollView(.horizontal, showsIndicators: false){
                                HStack(spacing: 10) {
                                    TabButton(title: String.i18nText("characters_popular"), selectedTab: $selectedTab){
                                        selectedTab = String.i18nText("characters_popular")
                                        viewModel.getData(category: "Popular")
                                    }
                                    TabButton(title: String.i18nText("characters_girlfriend"), selectedTab: $selectedTab){
                                        selectedTab = String.i18nText("characters_girlfriend")
                                        viewModel.getData(category: "AI Girlfriend")
                                    }
                                    TabButton(title: String.i18nText("characters_anime"), selectedTab: $selectedTab){
                                        selectedTab = String.i18nText("characters_anime")
                                        viewModel.getData(category: "Anime")
                                    }
                                    TabButton(title: String.i18nText("characters_games"), selectedTab: $selectedTab){
                                        selectedTab = String.i18nText("characters_games")
                                        viewModel.getData(category: "Game")
                                    }
                                    TabButton(title: String.i18nText("characters_celebrity"), selectedTab: $selectedTab){
                                        selectedTab = String.i18nText("characters_celebrity")
                                        viewModel.getData(category: "Celebrity")
                                    }
                                    TabButton(title: String.i18nText("characters_boyfriends"), selectedTab: $selectedTab){
                                        selectedTab = String.i18nText("characters_boyfriends")
                                        viewModel.getData(category: "AI Boyfriend")
                                    }
                                }
                                .background(Color.black.opacity(0.06))
                                .cornerRadius(15)
                            }
                            ForEach(viewModel.items, id: \.id) { item in
                                Button(action: {
                                    viewModel.logHistoryClick(roleId: "\(item.roleId)")
//                                    section = .second
                                    historyItemRoleId = item.roleId
                                    self.showingChatView = true
                                    //                                if item.iconString == "icon_robot" || item.iconString == "ai-character-elon-musk" {
                                    //                                    section = .second
                                    //                                    historyItemRoleId = item.roleId
                                    //                                } else if !viewModel.showVip {
                                    //                                    section = .second
                                    //                                    historyItemRoleId = item.roleId
                                    //                                } else {
                                    //                                    section = .second
                                    //                                    historyItemRoleId = item.roleId
                                    ////                                    viewModel.logfrom = "history"
                                    ////                                    viewModel.showSetting = true
                                    //                                }
                                }, label: {
                                    HistoryItemView(model: item)
                                        .frame(height: 80)
                                    //                                    .contentShape(Rectangle())
                                    //                                    .onTapGesture {
                                    //                                        if item.iconString == "icon_robot" || item.iconString == "icon_elon" {
                                    //                                            section = .second
                                    //                                            historyItemRoleId = item.roleId
                                    //                                        } else if !viewModel.showVip {
                                    //                                            section = .second
                                    //                                            historyItemRoleId = item.roleId
                                    //                                        } else {
                                    //                                            viewModel.showSetting = true
                                    //                                        }
                                    //                                    }
                                })
                            }
                        }
                        .padding()
                    }.fullScreenCover(isPresented: $showingChatView, content: {
                        ChatRoomView.init(rid: $historyItemRoleId, item: $item)
                    })
                }
                .fullScreenCover(isPresented: $viewModel.showSetting, content: {
                    VipView(viewModel: .init(from: viewModel.logfrom))
                })
                
                .onAppear {
                    viewModel.refreshUI(category: "Popular")
                }
            }
        })
    }
}

#Preview {
    HistoryView(viewModel: .init(), section: .constant(.first), historyItemRoleId: .constant(0))
}
